package com.biz.primus.ms.member.service;

import com.biz.primus.base.exception.BizBusinessException;
import com.biz.primus.base.exception.BizParameterException;
import com.biz.primus.model.member.vo.MemberSecretQuestionVo;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.member.dao.po.MemberSecretQuestion;
import com.biz.primus.ms.member.dao.redis.MemberSecretQuestionRedisDao;
import com.biz.primus.ms.member.dao.repository.MemberSecretQuestionRepository;
import com.biz.primus.ms.member.dao.ro.MemberRo;
import com.biz.primus.ms.member.dao.ro.MemberSecretQuestionRo;
import com.biz.primus.ms.member.trans.*;
import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

/**
 * 密保问题
 * @author jun.liu
 * @date 2017年10月23日
 * @reviewer
 * @see
 */
@Slf4j
@Service
public class MemberSecretQuestionService extends AbstractBaseService
{
    private final MemberSecretQuestionRedisDao memberSecretQuestionRedisDao;

    private final MemberSecretQuestionRepository memberSecretQuestionRepository;

    private final MemberService memberService;

    @Autowired
    public MemberSecretQuestionService(MemberSecretQuestionRedisDao memberSecretQuestionRedisDao, MemberSecretQuestionRepository memberSecretQuestionRepository,
                                       MemberService memberService)
    {
        this.memberSecretQuestionRedisDao = memberSecretQuestionRedisDao;
        this.memberSecretQuestionRepository = memberSecretQuestionRepository;
        this.memberService = memberService;
    }

    /**
     * 新增或者修改密保问题
     *
     * @param vo
     * @return
     * @author jun.liu
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(MemberSecretQuestionVo vo)
    {
        if (vo == null)
        {
            throw new BizParameterException("MemberSecretQuestionVo is null");
        }
        if (vo.getMemberId() == null)
        {
            throw new BizParameterException("MemberSecretQuestionVo memberId Parameter is null");
        }
        //服务中已经验证了空值和空对象 无须在进行判断
        MemberRo memberRo = this.memberService.findRoById(vo.getMemberId());
        MemberSecretQuestion po = new MemberSecretQuestionVoToMemberSecretQuestion().apply(vo);
        if(po.getId() == null || po.getId() == 0L)
        {
            po.setMemberId(memberRo.getId());
        }
        else
        {
            po.setUpdateTimestamp(new Timestamp(System.currentTimeMillis()));
        }
        saveOrUpdateUsingPoPKIncreament(memberSecretQuestionRepository, memberSecretQuestionRedisDao, po, new MemberSecretQuestionToMemberSecretQuestionRo());
    }

    /**
     * 通过 ID以及memberId获取密保问题
     *
     * @param id
     * @param memberId
     * @return
     * @author jun.liu
     */
    public MemberSecretQuestionVo getMemberSecretQuestion(Long id, Long memberId)
    {
        MemberSecretQuestionRo ro = memberSecretQuestionRedisDao.findOne(id);
        if (ro == null)
        {
            throw new BizBusinessException("MemberSecretQuestionRo findById query is null");
        }
        if (!Objects.equal(ro.getMemberId(), memberId)) {
            throw new BizBusinessException("无权查看别人的密保问题");
        }
        return new MemberSecretQuestionRoToMemberSecretQuestionVo().apply(ro);
    }

    /**
     * 删除
     * @author jun.liu
     */
    @Transactional(rollbackFor = Exception.class)
    public void delById(Long id)
    {
        if(id == null || id == 0L)
        {
            throw new BizParameterException("id Parameter is null");
        }
        this.deleteEntity(memberSecretQuestionRepository, memberSecretQuestionRedisDao, id);
    }

    public void save(List<MemberSecretQuestionVo> vos)
    {
        if(vos != null && !vos.isEmpty())
        {
            for (MemberSecretQuestionVo vo : vos)
            {
                save(vo);
            }
        }
    }

    public List<MemberSecretQuestionVo> findByMemberId(Long memberId) {
        List<MemberSecretQuestionRo> ros = memberSecretQuestionRedisDao.findByMemberId(memberId);
        return Lists.transform(ros, new MemberSecretQuestionRoToMemberSecretQuestionVo());
    }
}
